<?php

namespace Modules\Api\Http\Controllers;

use HiCommon\Repository\UsersRepository;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Api\Service\FeedbackService;
use Modules\Api\Service\H5Service;

error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);

class H5Controller extends Controller
{
    /**
     * 分享有礼
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function share(Request $request)
    {
        $uid = $request->get('uid', 0);
        $where['username'] = $uid;
        $option['field'] = ['id'];
        $user = UsersRepository::base_get_one($where, $option);
        if($user){
            $data = H5Service::share($user->id);
            $data['data']['invite'] = H5Service::my_invite($user->id);

            $share = FeedbackService::get_info($user->id);
            $data['data']['share'] = $share['data'];

            return view('api::share', $data['data']);
        }

        return view('api::share', []);
    }

    /**
     * 我要邀请的列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function my_invite(Request $request)
    {
        $uid = $request->get('uid', 0);
        $data = H5Service::my_invite($uid);

        return view('api::my_invite', $data['data']);
    }

    /**
     * 分享有礼注册页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function share_register(Request $request)
    {
        $user = $request->get('uid', '');

        return view('api::share_register', ['user' => $user]);
    }

    /**
     * 分享下载APP
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function share_app()
    {
        $data = H5Service::share_app();

        return view('api::share_app', $data);
    }

    /**
     * 门店加盟
     */
    public function help_shop_join()
    {
        try {
            $data = H5Service::help_shop_join();
            return view('api::help_shop_join', $data);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    /**
     * 客服服务->帮助中心
     */
    public function help_center()
    {
//        $type = $request->get('type', 0);

        try {
            $data = H5Service::help_center();
            return view('api::help_center', $data);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    /**
     * 客服服务->帮助中心/ 门店加盟
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function help_center_detail(Request $request)
    {

        try {
            $n_id = $request->get('id', 0);

            $data = H5Service::help_center_detail($n_id);

            return view('api::help_center_detail',$data);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    /**
     * 保养手册
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function handbook()
    {
        $data = H5Service::handbook();

        return view('api::handbook', $data);
    }

    /**
     * 资讯详情
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function news(Request $request)
    {
        $id = (int)$request->get('id', 0);
        $uid = (int)$request->get('uid', 0);

        $data = H5Service::news($id, $uid);
        $data['data']['user_id'] = $uid > 0 ? $uid : time();//当前用户id

        return view('api::news', $data);
    }

    /**
     * Ajax获取评论列表
     * @param Request $request
     * @return false|string
     */
    public function comment(Request $request)
    {
        $id = (int)$request->get('id', 0);
        $uid = (int)$request->get('uid', 0);
        $page = (int)$request->get('page', 0);

        $data = H5Service::comment($id, $uid, $page);

        return json_encode($data);
    }

    /**
     * 删除评论
     * @param Request $request
     * @return false|string
     * @throws \Exception
     */
    public function zan_comment_del(Request $request)
    {
        $data = $request->all();

        shiwu_start();
        $result = H5Service::zan_comment_del($data);
        shiwu_end($result);

        return json_encode($result);
    }
}
